<?php
class DataObjectManager {
    private $DO;
    //private $EDO;
    //private $EDOPointer = 0;
    private static $instance;

    /**
     * This function registers a new object with the object manager for ease of
     * reference
     * 
     * @param object $object - A new object of subclass DataObject
     * @return null
     */
    public function registerDataObject(&$object) { //This'll take a empty or assembled dataobject that has been commited and register it in the object manager
        
        //print_r($object);
        $this->DO[$object->type][$object->id] = clone $object;
        $object = $this->DO[$object->type][$object->id];
        return;
    }
    
    /*public function makeEmptyDataObject($type) {
        $this->EDO[$this->EDOPointer] = new $type();
        $r = &$this->EDO[$this->EDOPointer];
        $this->EDOPointer++;
        return $r;        
    }*/    //Not entirely sure this function is actually useful
        //Could just use new type() and then Register it when done - AS

    /**
     *
     * @param string $type - The data object name as a string
     * @param int $id - The objects ID
     * @return object
     */
    public function fetchDataObject($type,$id,$id2=null) {
        $idt = $id;
        if (! is_null($id2)) {
            $idt = "$id - $id2";
        }
        if (isset($this->DO[$type][$idt]) && $this->DO[$type][$idt] instanceof DataObject) {
            $r = &$this->DO[$type][$idt];
        }
        else {
            $r = $this->fetchDataObjectFromDB($type, $id, $id2);
            $r->link();
        }
        return $r;
    }
    /**
     *
     * @param string $type - The data object name as a string
     * @param int $id - The object's ID
     * @return object
     */
    private function fetchDataObjectFromDB($type, $id, $id2=null) {
        $idt = $id;
        if (! is_null($id2)) {
            $idt = "$id - $id2";
        }
        $this->DO[$type][$idt] = new $type();
        $r= &$this->DO[$type][$idt];
        $r->fetchFromDB($id,$id2);
        return $r;
    }
    
    private function __construct() {
        $this->DO = Array(Array()); //Sets DO object as a two-dimensional array
        $this->EDO = Array();
        return;
    }

    /**
     * Retrieve the singleton instance of the object
     * 
     * @return DataObjectManager
     */
    public static function inst() {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }
    
    public function debugDump() {
        echo ('<pre>');
        print_r($this);
        echo ('</pre>');
    }
}
?>